package com.situ.company.score.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONObject;

import com.situ.company.score.model.ScoreModel;
import com.situ.company.score.service.ScoreService;
import com.situ.company.util.FmtRequest;

@WebServlet("/score")
public class ScoreServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	private ScoreService service = new ScoreService();

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String action = req.getParameter("action");
		String result = "";
		switch (action) {
		case "add":
			result = add(req);
			break;
		case "sel":
			result = sel(req);
			break;
		case "del":
			result = del(req);
			break;
		case "get":
			result = get(req);
			break;
		case "upd":
			result = upd(req);
			break;
		case "list":
			result = list(req);
			break;
		}
		FmtRequest.out(resp, result);
	}

	/**
	 * 分页
	 * 
	 * @param req
	 * @return
	 */
	private String list(HttpServletRequest req) {
		String codeEmp = req.getParameter("codeEmp");// 获取参数
		String codePro = req.getParameter("codePro");
		String pageIndex = req.getParameter("pageIndex");// 获取参数
		String pageLimit = req.getParameter("pageLimit");
		ScoreModel model = new ScoreModel(codeEmp, codePro);
		model.setPageIndex(Integer.parseInt(pageIndex));
		model.setPageLimit(Integer.parseInt(pageLimit));
		model.setPageOn(true);
		Map<String, Object> map = new HashMap<>();
		map.put("data", service.selectList(model));
		map.put("count", service.selectCount(model));
		return new JSONObject(map).toString();
	}

	/**
	 * 修改
	 * 
	 * @param req
	 * @return
	 */
	private String upd(HttpServletRequest req) {
		Integer id = Integer.parseInt(req.getParameter("id"));
		String codeEmp = req.getParameter("codeEmp");
		String codePro = req.getParameter("codePro");
		String score = req.getParameter("score");
		ScoreModel model = new ScoreModel(id, codeEmp, codePro, score);
		return service.update(model);
	}

	/**
	 * 修改回显
	 * 
	 * @param req
	 * @return
	 */
	private String get(HttpServletRequest req) {
		Integer id = Integer.parseInt(req.getParameter("id"));
		ScoreModel model = new ScoreModel(id);
		ScoreModel mdb = service.selectId(model);
		return new JSONObject(mdb).toString();
	}

	/**
	 * 删除
	 * 
	 * @param req
	 * @return
	 */
	private String del(HttpServletRequest req) {
		Integer id = Integer.parseInt(req.getParameter("id"));
		return service.delete(id);
	}

	/**
	 * 查询
	 * 
	 * @param req
	 * @return
	 */
	private String sel(HttpServletRequest req) {
		String codeEmp = req.getParameter("codeEmp");
		String codePro = req.getParameter("codePro");
		ScoreModel model = new ScoreModel(codeEmp, codePro);
		List<ScoreModel> list = service.selectList(model);
		return new JSONArray(list).toString();
	}

	/**
	 * 添加
	 * 
	 * @param req
	 * @return
	 */
	private String add(HttpServletRequest req) {
		String codeEmp = req.getParameter("codeEmp");
		String codePro = req.getParameter("codePro");
		String score = req.getParameter("score");
		ScoreModel model = new ScoreModel(codeEmp, codePro, score);
		return service.insert(model);
	}
}
